/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.j4sc;

import org.apache.commons.lang.StringUtils;

/**
 * The PartyInformation class is used to store details for the calling and called party.
 * A null PartyInformation object will result in no party information
 * being sent in the submission to SpinVox create.<p>
 * 
 * Likewise, if any of the attributes such as called party name are set to null which
 * they are by default, they will not be included in the submission.<p>
 * 
 * However if you set an attribute to an empty string, an empty string will be sent 
 * for the value of this attribute in the submission.<p>
 * 
 * @author Raj Patel, Dariusz Lorenc
 */
public class PartyInformation {

	private static final int NAME_MAX_LENGTH = 64;
	private static final int IDENTIFIER_MAX_LENGTH = 20;
	
	private String callingPartyName;
	private String callingPartyIdentifier;
	private String calledPartyName;
	private String calledPartyIdentifier;

	/**
	 * The calling party name
	 * 
	 * @param name the calling party name, max length is 64 characters
	 * @throws IllegalArgumentException 
	 * 			thrown if invalid value is provided
	 */
	public void setCallingPartyName(String name) throws IllegalArgumentException {
		validateLength(name, NAME_MAX_LENGTH, "Calling party name");
		this.callingPartyName = name;
	}

	/**
	 * The calling party identifier
	 * 
	 * @param identifier the calling party identifier, max length is 20 characters
	 * @throws IllegalArgumentException 
	 * 			thrown if invalid value is provided
	 */
	public void setCallingPartyIdentifier(String identifier) throws IllegalArgumentException {
		validateLength(identifier, IDENTIFIER_MAX_LENGTH, "Calling party identifier");
		this.callingPartyIdentifier = identifier;
	}

	/**
	 * The called party name
	 * 
	 * @param name the called party name, max length is 64 characters
	 * @throws IllegalArgumentException 
	 * 			thrown if invalid value is provided
	 */
	public void setCalledPartyName(String name) throws IllegalArgumentException {
		validateLength(name, NAME_MAX_LENGTH, "Called party name");
		this.calledPartyName = name;
	}

	/**
	 * The called party identifier
	 * 
	 * @param identifier the called party identifier, max length is 20 characters
	 * @throws IllegalArgumentException 
	 * 			thrown if invalid value is provided
	 */
	public void setCalledPartyIdentifier(String identifier) throws IllegalArgumentException {
		validateLength(identifier, IDENTIFIER_MAX_LENGTH, "Called party identifier");
		this.calledPartyIdentifier = identifier;
	}

	/**
	 * The calling party name
	 * 
	 * @return the calling party name
	 */
	public String getCallingPartyName() {
		return callingPartyName;
	}

	/**
	 * The calling party identifier
	 * 
	 * @return the calling party identifier
	 */
	public String getCallingPartyIdentifier() {
		return callingPartyIdentifier;
	}

	/**
	 * The called party name
	 * 
	 * @return the called party name
	 */
	public String getCalledPartyName() {
		return calledPartyName;
	}

	/**
	 * The called party identifier
	 * 
	 * @return the called party identifier
	 */
	public String getCalledPartyIdentifier() {
		return calledPartyIdentifier;
	}
	
	private void validateLength(String value, int maxLength, String fieldName) {
		if (StringUtils.isNotBlank(value) && value.length() > maxLength) {
			throw new IllegalArgumentException(String.format("%s cannot be greater than %s characters", fieldName, maxLength));
		}
	}

	/**
	 * Is the party details empty?
	 *
	 * @return true if none of the details are set
	 */
	public boolean isEmpty() {
		return isCalledPartyEmpty() && isCallingPartyEmpty();
	}
	
	/**
	 * Is the called party details empty?
	 * 
	 * @return true if none of the called party details are set
	 */
	public boolean isCalledPartyEmpty() {
		return StringUtils.isBlank(calledPartyIdentifier) && StringUtils.isBlank(calledPartyName);
	}

	/**
	 * Is the calling party details empty?
	 * 
	 * @return true if none of the calling party details are set
	 */
	public boolean isCallingPartyEmpty() {
		return StringUtils.isBlank(callingPartyIdentifier) && StringUtils.isBlank(callingPartyName);
	}
}
